package com.yc.common.utils;

import java.util.ArrayList;
import java.util.Collections;

public class CaptchaGeneratorUtil {

    private static final Integer NUMBER = 48;
    private static final Integer UPPER = 65;
    private static final Integer LOWER = 97;


    public static StringBuilder extracted() {

        ArrayList<String> list = new ArrayList<>();
        for (int i = NUMBER; i < (NUMBER + 10); i++) {
            list.add((char)i + "");
        }
        for (int i = UPPER; i < (UPPER + 26); i++) {
            list.add((char)i + "");
        }
        for (int i = LOWER; i < (LOWER + 26); i++) {
            list.add((char)i + "");
        }

        Collections.shuffle(list);
        StringBuilder captcha = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            if (i == 3) {
                if (isInteger(list.get(3))) {
                    captcha.append(list.get(3));
                } else {
                    Collections.shuffle(list);
                    i = 2;
                }
            } else {
                captcha.append(list.get(i));
            }
        }
        return captcha;
    }

    private static boolean isInteger(String str) {
        try {
            Integer.parseInt(str); // 尝试将字符串转换为整数
            return true; // 转换成功，说明是整数
        } catch (NumberFormatException e) {
            return false; // 转换失败，说明不是整数
        }
    }
}
